//
//  LivesNetworkTool.m
//  Lives
//
//  Created by 花化华 on 2017/1/14.
//  Copyright © 2017年 itcast. All rights reserved.
//

#import "LivesNetworkTool.h"

@implementation LivesNetworkTool

+ (instancetype)sharedNetworkTool{
    static LivesNetworkTool *instance = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        // 实例化Manager
        instance = [[LivesNetworkTool alloc]init];
        
        instance.requestSerializer = [[AFJSONRequestSerializer alloc]init];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    });
    
    return instance;
}

- (void)GETWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    [self GET:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * task, NSDictionary * responseObject) {
        // AFN的回调,默认就是在主线程
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}

- (void)POSTWithURLString:(NSString *)URLString parameters:(NSDictionary *)parameters success:(void(^)(id responseObject))success faile:(void(^)(NSError *error))faile{
    
    [self POST:URLString parameters:parameters progress:nil success:^(NSURLSessionDataTask * task, NSDictionary * responseObject) {
        // AFN的回调,默认就是在主线程
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (faile) {
            faile(error);
        }
    }];
}

@end
